package com.android.multiplesmart.activity;


import static android.Manifest.permission.INTERNET;
import static android.Manifest.permission.RECORD_AUDIO;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;


import com.android.multiplesmart.DemoApplication;
import com.android.multiplesmart.bean.Servicess;
import com.android.multiplesmart.utils.ActivityUtils;
import com.schillerchina.baselibrary.utils.ToastUtil;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * @author zhangzhiguo
 * @date 2019-08-05 09:39
 * @Description: Activity基础类
 */
public abstract class BaseActivity extends FragmentActivity implements EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks, View.OnClickListener {
//    Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.BLUETOOTH,
//    Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET,
//    Manifest.permission.ACCESS_COARSE_LOCATION,
//    Manifest.permission.READ_PHONE_STATE,
    public final static String[] PERMS_WRITE = {
        Manifest.permission.RECORD_AUDIO,
//        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.CAMERA,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_WIFI_STATE,
//      Manifest.permission.READ_EXTERNAL_STORAGE,
//      Manifest.permission.WRITE_EXTERNAL_STORAGE,
//      Manifest.permission.READ_MEDIA_IMAGES,
        Manifest.permission.INTERNET,
        Manifest.permission.ACCESS_NETWORK_STATE,

    };


//        <uses-permission android:name="android.permission.CAMERA" /> <!-- WebView定位功能权限 -->
//    <!-- 粗略定位权限，允许一个程序通过网络获取粗略的位置 -->
//    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 精确定位权限，允许一个程序访问精确位置(GPS定位) -->
//    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- debugtbs.qq.com 安装线上内核需要权限 -->
//    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 写入tbslog.txt需要权限，否则通过QbSdk.setTbsLogClient()接管解决 -->
//    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- TBS基础权限需求 -->
//    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- TBS基础权限需求 -->
//    <uses-permission android:name="android.permission.INTERNET" />
//    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏
        ActivityUtils.addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        DemoApplication.setCurrentActivity(this);
        if (this instanceof SelectLanguageActivitys){
            Servicess.getService().setState(1);
        }else {
            Servicess.getService().setState(2);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtils.removeActivity(this);
    }

    /**
     * @param context return true:已经获取权限
     *                return false: 未获取权限，主动请求权限
     */

    public static boolean checkPermission(Activity context, String[] perms) {
        return EasyPermissions.hasPermissions(context, perms);
    }

    /**
     * 请求权限
     *
     * @param context
     */
    public static void requestPermission(Activity context, String tip, int requestCode, String[] perms) {
        EasyPermissions.requestPermissions(context, tip, requestCode, perms);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //将请求结果传递EasyPermission库处理
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    /**
     * 请求权限成功。
     * 可以弹窗显示结果，也可执行具体需要的逻辑操作
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {

    }

    /**
     * 请求权限失败
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

        ToastUtil.showToast(getApplicationContext(), "用户授权失败");

//        requestPermission(this, "需要打开麦克风权限", 100, PERMS_WRITE);
        /**让用
         *          * 若是在权限弹窗中，用户勾选了'NEVER ASK AGAIN.'或者'不在提示'，且拒绝权限。
         *          * 这时候，需要跳转到设置界面去，户手动开启。
         */
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {

            new AppSettingsDialog.Builder(this).build().show();
        }

    }


    /**
     * 接受请求某些权限
     *
     * @param requestCode
     */
    @Override
    public void onRationaleAccepted(int requestCode) {

    }

    /**
     * 拒绝某些权限
     *
     * @param requestCode
     */
    @Override
    public void onRationaleDenied(int requestCode) {
        finish();
    }

    @Override
    public abstract void onClick(View v);


    public abstract void initListener();

    public abstract void initView();

    public void startActivity(Class<?> cls) {
        startActivity(new Intent(this, cls));
    }


}
